package LogisticsSystem.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import LogisticsSystem.po.Cargo;
import LogisticsSystem.po.Customer;
import LogisticsSystem.po.Staff;
import LogisticsSystem.po.ext.DispatchExtend;
import LogisticsSystem.service.CargoService;
import LogisticsSystem.service.CustomerService;
import LogisticsSystem.service.DispatcherService;
import LogisticsSystem.service.UserService;

@Controller
@RequestMapping("user")
public class UserController {// 登录，注销，注册

	@Autowired
	private UserService userService;

	@Autowired
	private CustomerService customerService;

	@Autowired
	private CargoService cargoService;

	@Autowired
	private DispatcherService dispatcherService;

	/**
	 * 登录验证,传入手机帐号密码，并且选择一个身份，值customer表示顾客，other表示其他
	 * 
	 * @param phone
	 * @param password
	 * @param role
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public ModelAndView validateLogin(String phone, String password, HttpServletRequest request) throws Exception {
		ModelAndView modelAndView = new ModelAndView();
		if (phone != null && password != null) {
			Customer customer = userService.customerlogin(phone, password);
			if (customer != null) {
				// 验证成功
				request.getSession().setAttribute("onlineUser", customer);
				modelAndView.setViewName("redirect:/index.jsp");// 跳转
				request.getSession().setAttribute("recevierList", customerService.showReceiver(customer.getcId()));
				request.getSession().setAttribute("CargoList", cargoService.getCargolist(null));

				List<DispatchExtend> DEList = dispatcherService.selectDispatchExtendByCId(customer.getcId());
				request.getSession().setAttribute("DEList", DEList);
				// request.getSession().setAttribute("orderList",customerService.showOrder(customer.getcId()));
				// request.getSession().setAttribute("orderDetailList",customerService.showOrder(customer.getcId()));
				return modelAndView;
			}
		}
		modelAndView.addObject("error", "用户或密码错误！");
		modelAndView.setViewName("redirect:/index.jsp");
		return modelAndView;
	}

	// 注销
	@RequestMapping("/logout")
	public String logout(HttpSession session) {
		session.invalidate();
		return "redirect:/index";//
	}

	// 跳转注册
	@RequestMapping("/register")
	public String registerJump() throws Exception {

		return "";//
	}

	/**
	 * 注册保存，仅能注册顾客用户
	 * 
	 * @param customer,名字，电话（账户），密码
	 * @param password1重新输入密码
	 * @param model
	 * @param attribute
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/store")
	public String insertUser(Model model, HttpServletRequest request) throws Exception {
		boolean isEqual = true;
		Customer customer = new Customer();
		customer.setcName(request.getParameter("c_name"));
		customer.setcPhone(request.getParameter("c_phone"));
		customer.setcPwd(request.getParameter("c_pwd"));
		String password1 = request.getParameter("password1");

		if (customer.getcPwd() != null & customer.getcPwd().equals(password1)) {
			isEqual = true;
		} else {
			isEqual = false;
		}
		boolean notUnique = userService.findCustomer(customer.getcPhone());
		if (isEqual && !notUnique && userService.insertCustomer(customer)) {
			request.getSession().setAttribute("onlineUser", customer);
			return "redirect:/index.jsp";// 返回注册后该顾客的主页
		}
		model.addAttribute("error", "error");
		if (notUnique) {
			model.addAttribute("tip", "用户名已存在");
		} else if (!isEqual) {
			model.addAttribute("tip", "密码不一致");
		} else {
			model.addAttribute("tip", "");//
		}
		return "redirect:/index.jsp";//
	}

	/**
	 * 查看所有商品
	 * 
	 * @return
	 */
	@RequestMapping("/getAllCargos")
	public ModelAndView selectAllCargo() {
		ModelAndView modelAndView = new ModelAndView();
		List<Cargo> list = cargoService.getCargolist(null);
		modelAndView.addObject("cargoList", list);
		modelAndView.setViewName("/ListCargo");
		return modelAndView;
	}

	@RequestMapping("/staffLogin")
	public ModelAndView staffLogin(HttpServletRequest request, HttpServletResponse response) {
		String phone = request.getParameter("userName");
		String password = request.getParameter("pwd");
		Staff staff = null;

		try {
			staff = userService.stafflogin(phone, password);
			if (staff != null) {
				String role = staff.getsRole();
				System.out.println(role);
				request.getSession().setAttribute("staff", staff);
				if (role.equals("1")) {// 中心库存管理页面
					return new ModelAndView("redirect:/cargo/searchInventory", null);
				} else if (role.equals("2") || role.equals("3")) {// 分站库存管理员
					return new ModelAndView("redirect:/dispatcher/selectDispatchsBySId", null);
				}else if (role.equals("4")) {// 客服
					return new ModelAndView("redirect:/serviceStaff/showOrder", null);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("error");
		}
		return new ModelAndView("redirect:/Backstage", null);
	}

}
